package zzq.upload;

import zzq.util.FtpConfig;
import zzq.util.Properties;

import java.io.*;

public class UploadFtpConfigImpl implements FtpConfig {
    @Override
    public Properties loadProperties() {
        Properties properties = new Properties();
        try {
            BufferedReader reader = new BufferedReader(new FileReader(new File("./config.txt")));
            String s = reader.readLine();
            while (s != null) {
                handleLine(s, properties);
                s = reader.readLine();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return properties;
    }

    private void handleLine(String s, Properties properties) {
        if (s.startsWith("uploadHost=")) {
            properties.setHost(s.substring("uploadHost=".length()));
        }
        if (s.startsWith("serverEncoding=")) {
            properties.setServerEncoding(s.substring("serverEncoding=".length()));
        }
        if (s.startsWith("uploadPort=")) {
            String a = s.substring("uploadPort=".length());
            int b = properties.getPort();
            try {
                b = Integer.valueOf(a);
            } catch (Exception e) {

            } finally {
                properties.setPort(b);
            }
        }
        if (s.startsWith("detectCycle=")) {
            String a = s.substring("detectCycle=".length());
            int b = properties.getDetectCycle();
            try {
                b = Integer.valueOf(a);
            } catch (Exception e) {

            } finally {
                properties.setDetectCycle(b);
            }
        }
        if (s.startsWith("uploadUserName=")) {
            properties.setUserName(s.substring("uploadUserName=".length()));
        }
        if (s.startsWith("uploadPassword=")) {
            properties.setPassword(s.substring("uploadPassword=".length()));
        }
        if (s.startsWith("uploadFolder=")) {
            properties.setFolder(s.substring("uploadFolder=".length()));
        }
    }
}
